package com.qing.swagger.demo.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.PathProvider;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启swagger
public class SwaggerConfig {


//	@Value("${swagger.show}")
//	private boolean swaggerShow;

	@Bean
	public Docket docket(Environment environment){
		Boolean flag=false;
	//设置要显示的swagger环境
	Profiles profiles=Profiles.of("dev","test");
	//Environment.acceptsProfiles 判断是否处在，自己设定的环境中
	if(environment.acceptsProfiles(profiles)){
		flag=true;
	}

	return  new Docket(DocumentationType.SWAGGER_2)
			//关闭
			.enable(flag)
			.groupName("轻羽")
				.apiInfo(apiInfo()).select()
			//指定扫描的方式
			// basePackage: 扫描指定的包
			//  any()： 扫描全部
			//  none(): 都不扫描
			// withMethodAnnotation()  扫描方法上的注解
			// withClassAnnotation  扫描类上的注解
			.apis(RequestHandlerSelectors.basePackage("com.qing.swagger.demo"))
//			.apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
//			.apis(RequestHandlerSelectors.withClassAnnotation(Controller.class))
			//过滤路径
			//.paths(PathSelectors.ant("/hello/**"))
			.build();
	}


	//多个分组
	@Bean
	public Docket docket2(){
		return  new Docket(DocumentationType.SWAGGER_2).groupName("轻羽2");
	}

	@Bean
	public Docket docket3(){
		return  new Docket(DocumentationType.SWAGGER_2).groupName("轻羽3");
	}

	//配置swagger信息
	@Bean
	public ApiInfo apiInfo(){
		Contact contact = new Contact("qing", "http://localhost:8080/swagger-ui.html", "zhipeng.cao");
		return  new ApiInfo("Api 文档名称",
				"Api 文档,这是一个博客",
				"1.0",
				"http://localhost:8080/swagger-ui.html",
				contact, "Apache 2.0",
				"http://www.apache.org/licenses/LICENSE-2.0",
				new ArrayList());
	}
}
